* Aggregate analyses

***** Beliefs

cd "$pathdata/Beliefs/"

use Beliefs_cleaned, clear


*Keep Baseline Rounds
keep if baseline_set == 1

replace bayesian_posterior = bayesian_posterior/100
replace belief = belief/100

gen delta=.
gen gamma=.
gen beta=.
gen default=.
gen alpha=.


**** Manually generate AIC for belief ~ N(bayesian_posterior, sigma^2)

* Generate Residuals
gen res = belief - bayesian_posterior

* Generate residual error
gen sigma2 = res*res
egen sigma = mean(sigma2)
replace sigma = sqrt(sigma)

* Generate likelihoods
gen like = normalden(res, 0, sigma)

*Generate Log-Likelihood
gen llike = ln(like)

*Generate AIC
egen AIC = total(llike)

replace AIC = -2*AIC

tab AIC

// * With CU
nl (belief = (1-min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))))* {default=1} + min(1,max(0,(1-{gamma=1}*cognitive_uncertainty))) * bayesian_posterior), cl(id)
predict pred_cu
estat ic

preserve
foreach i in pred_cu bayesian_posterior belief {
	replace `i'=`i'*100
}


gen pred_restricted = bayesian_posterior
*Following Ben's instruction to round to the nearest 5
replace bayesian_posterior = round(bayesian_posterior,5)

collapse (median) belief cognitive_uncertainty pred_cu pred_restricted (semean) se_ce=belief se_cu=pred_cu  (count) no = belief, by(bayesian_posterior)

gen upper=belief+se_ce
gen lower=belief-se_ce
gen upper_cu=pred_cu+se_cu
gen lower_cu=pred_cu-se_cu

drop if no<30

tw (scatter belief bayesian_posterior, msize(large)  ms(+) mcolor(black)) ///
	(line pred_cu bayesian_posterior, lwidth(medthick) color(red%80))  ///
 	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Subjective Posterior Belief") ///
    xtitle("Bayesian Posterior") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2) label(1 "Subject Data") label(2 "CU Model") r(2))


